package com.huonan.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.hive.jdbc.HiveStatement;

import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class HiveLogCallable implements Callable<String> {
	private HiveStatement hiveStatement;
	public HiveLogCallable(HiveStatement hiveStatement) { this.hiveStatement = hiveStatement; }
	private String updateQueryLog(){
		String yarnAppId ="";
		try {
			List<String> queryLogs = hiveStatement.getQueryLog();
			for (String hiveLog : queryLogs) {
				if (hiveLog.contains("FileCompareApp id")) {
					yarnAppId  = hiveLog.substring(hiveLog.indexOf("FileCompareApp id") + 7, hiveLog.length() - 1);
					log.info("hive sql 执行进度信息--> "+yarnAppId );
				}
			}
		} catch (Exception e){ log.error(e.getLocalizedMessage()); }
		return yarnAppId ;
	}
	@Override
	public String call() throws Exception {
		String yarnAppId = null;
		try {
			while (hiveStatement.hasMoreLogs()) {
				yarnAppId = updateQueryLog();
				if (yarnAppId!="") { break; }
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) { log.error(e.getLocalizedMessage()); }
		return yarnAppId;
	}
}